package cn.dengta.webapp.trade.dao;

import java.math.BigDecimal;
import java.util.Date;

import cn.dengta.common.model.TimeInterval;
import cn.dengta.webapp.base.dao.BaseDao;
import cn.dengta.webapp.trade.entity.UserIntegralDailyStat;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.cursor.Cursor;

/**
 * 用户积分每日汇总 Mapper 接口
 *
 * @author lizhichao
 * @since 2024-01-03
 */
public interface UserIntegralDailyStatDao extends BaseDao<UserIntegralDailyStat> {

	@Update("INSERT INTO tr_user_integral_daily_stat (user_id,date,${field},add_time,update_time) " +
			"VALUES (#{userId},#{date},#{tradeAmount},#{now},#{now}) " +
			"ON DUPLICATE KEY UPDATE ${field} = ${field} + #{tradeAmount},update_time = #{now}")
	int addIncome(@Param("userId") Long userId, @Param("field") String field, @Param("tradeAmount") BigDecimal tradeAmount, @Param("date") String date, @Param("now") Date now);

	@Update("UPDATE tr_user_integral_daily_stat " +
			"SET ${field} = ${field} + #{tradeAmount},update_time = #{now} " +
			"WHERE user_id = #{userId} AND date = #{date}")
	int tryAddIncome(@Param("userId") Long userId, @Param("field") String field, @Param("tradeAmount") BigDecimal tradeAmount, @Param("date") String date, @Param("now") Date now);

	@Select("SELECT " +
			"IFNULL(SUM(text_income + gift_income + video_income + audio_income),0) " +
			"FROM tr_user_integral_daily_stat " +
			"WHERE user_id = #{userId} AND date BETWEEN #{interval.finalBegin} AND #{interval.finalEnd}")
	BigDecimal getTotalIncome(@Param("userId") Long userId, @Param("interval") TimeInterval interval);

	@Select("SELECT ids.user_id,SUM(ids.text_income + ids.video_income + ids.audio_income + ids.gift_income) totalIncome " +
			"FROM tr_user_integral_daily_stat ids JOIN dt_user du ON ids.user_id = du.id " +
			"WHERE ids.date BETWEEN #{interval.finalBegin} AND #{interval.finalEnd} AND du.gender = 0 " +
			"GROUP BY ids.user_id " +
			"HAVING totalIncome >= #{minIncome}")
	Cursor<UserIntegralTotal> cursorIncome(@Param("interval") TimeInterval interval, @Param("minIncome") Integer minIncome);

	@Select("SELECT ids.user_id,SUM(ids.text_income + ids.video_income + ids.audio_income + ids.gift_income) totalIncome " +
			"FROM tr_user_integral_daily_stat ids JOIN dt_user du ON ids.user_id = du.id " +
			"WHERE ids.date = #{date} AND du.gender = 0 AND du.add_time >= #{newTime} " +
			"GROUP BY ids.user_id " +
			"HAVING totalIncome >= #{minIncome}")
	Cursor<UserIntegralTotal> cursorNewUserIncome(@Param("newTime") Date newTime, @Param("date") Date date, @Param("minIncome") Integer minIncome);

	@Select("SELECT ids.user_id,SUM(ids.text_income + ids.video_income + ids.audio_income + ids.gift_income) totalIncome " +
			"FROM tr_user_integral_daily_stat ids JOIN dt_user du ON ids.user_id = du.id " +
			"WHERE ids.date = #{date} AND du.gender = 0 AND du.add_time < #{oldTime} " +
			"GROUP BY ids.user_id " +
			"HAVING totalIncome >= #{minIncome}")
	Cursor<UserIntegralTotal> cursorOldUserIncome(@Param("oldTime") Date oldTime, @Param("date") Date date, @Param("minIncome") Integer minIncome);

	class UserIntegralTotal {

		public Long userId;
		public BigDecimal totalIncome;

	}

}
